package linuxbox.download.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.text.*;

import linuxbox.BoxStatic;
import linuxbox.download.logic.*;
import linuxbox.download.bean.*;

/**
 * This class displays a department list.
 */
public class DlFileList extends TagSupport {
  protected String listType = null;
  protected String sortType = "NAME";
  protected String userType = "NORMAL";
  protected int selected = -1;

  private static final String SORT_BY_NAME = "NAME";
  private static final String SORT_BY_SIZE = "SIZE";
  private static final String SORT_BY_TIME = "TIME";
  private static final String SORT_BY_CATE = "CATE";

  private static final String USER_TYPE_NORMAL = "NORMAL";
  private static final String USER_TYPE_ADMIN = "ADMIN";

  private static final String LIST_TYPE_TABLE = "TABLE";
  private static final String LIST_TYPE_OPTION = "ALL";

  public int doStartTag() throws JspException {
    try {
      DownLoadManager dlm = DownLoadManager.getInstance();
      Iterator ids = null;

      if (SORT_BY_NAME.equals(sortType)) {
        ids = dlm.getAllDlFiles(" order by name");
      } else if (SORT_BY_SIZE.equals(sortType)) {
        ids = dlm.getAllDlFiles(" order by file_size");
      } else if (SORT_BY_TIME.equals(sortType)) {
        ids = dlm.getAllDlFiles(" order by upload_date");
      } else if (SORT_BY_CATE.equals(sortType)) {
        ids = dlm.getAllDlFiles(" order by cat_id");
      } else {
        ids = dlm.getAllDlFiles();
      }

      JspWriter out = pageContext.getOut();

      DownLoadObject obj = null;
      DownLoadKey key = null;

      if (LIST_TYPE_OPTION.equalsIgnoreCase(listType)) {
        StringBuffer retVal = new StringBuffer();

        while (ids.hasNext()) {
          key = (DownLoadKey)ids.next();
          obj = dlm.getDownLoad(key);

          retVal.append("<OPTION value=" + obj.getID());

          if (obj.getID() == selected) {
            retVal.append(" SELECTED");
          }

          retVal.append(">" + obj.getName() + "</OPTION>");
        }
        out.println(retVal.toString());
      } else if (LIST_TYPE_TABLE.equalsIgnoreCase(listType)) {
        StringBuffer retVal = new StringBuffer();

        int i = 0;
        while (ids.hasNext()) {
          key = (DownLoadKey)ids.next();
          obj = dlm.getDownLoad(key);

          retVal.append("<TR");

          if ((i % 2) != 0)
            retVal.append(" bgcolor=\"#E0E0E0\">");
          else
            retVal.append(">");

          retVal.append("<TD><A href=\"" + BoxStatic.contextName
            + BoxStatic.DOWNLOAD_FILE_DIR + obj.getFileName() + "\">"
            + obj.getName() + "</A></TD>");
          retVal.append("<TD>" + obj.getFileSize() + "</TD>");

          SimpleDateFormat formatter =
            new SimpleDateFormat(BoxStatic.NORMAL_DATETIME_FORMAT);
          String dateString = formatter.format(
            (Date)obj.getUploadDate());
          retVal.append("<TD>" + dateString + "</TD>");
          retVal.append("<TD>" + obj.getCatName() + "</TD>");

          if (USER_TYPE_ADMIN.equalsIgnoreCase(userType)) {
              retVal.append("<TD><a href=\""+BoxStatic.servletDir+"DlFileDelAction?DL_FILE_ID="
                + obj.getID() + "\">Delete</a></TD>");
                    }

          retVal.append("</TR>");

          i++;
        }
        out.println(retVal.toString());
      }

      out.flush();
    } catch(Exception e) {
      BoxStatic.logger.debug("Failed to get all users");
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setListType(String aValue) {
    listType = aValue;
  }

  public void setSelected(String aValue) {
    selected = Integer.parseInt(aValue);
  }

  public void setSortType(String aStr) {
    sortType = aStr;
  }

  public void setUserType(String aStr) {
    userType = aStr;
  }
}
